$(document).ready(function() {

	numberMask();
	
	if($("#materialDTO_id").val() == "")
		prepareView();
	
	var options = {
		target : '#list-materiales',
		data : {},
		success : onSuccessfullyAction
	};

	$('#material-form').ajaxForm(options);

	$("#button-close").click(function(event) {
		event.preventDefault();
		$("#form-content").empty();
	});

	$("#button-delete").click(function(event) {
		event.preventDefault();
		if ($("#materialDTO_id").val() != "")
			$.post("/materiales/delete", {
				id : $("#materialDTO_id").val()
			}, function(data) {
				$("#list-materiales").html(data);
				onSuccessfullyAction();
			});
		else {
			clear($('#material-form'));
			showCorrectFields();
		}
	});

	$("#materialDTO_tipo").change(function() {
		
		showCorrectFields();
	});
	
	$("select").uniform();
});

function onSuccessfullyAction() {
	oTable = initHiddenColumnDataTable($("#materiales-table"));
	clear($('#material-form'));
	showCorrectFields();
	//prepareView();
}

//function clear() {
//	$('#material-form').resetForm();
//	$('#material-form').clearForm();
//	//$('select option:first-child').attr("selected", "selected");
//}

function prepareView() {
	$("#materialDTO_temperatura").val('0');
	$("#materialDTO_espesor").val('0');
	$("#field-temperatura").addClass('hidden');
	$("#field-espesor").addClass('hidden');
	$("#field-acabado").removeClass('hidden');
}
